package com.escafoide.smm;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.escafoide.smm.data.datasamples;

public class graphs extends Activity implements OnGesturePerformedListener {
	GestureLibrary mLibrary;
	ImageView bar1;
	ImageView bar11;
	ImageView bar2;
	ImageView bar22;
	ImageView imgconfig;
	TextView textviewgroc;
	TextView textviewblau;
	TextView textviewblau2;
	
	int posactual = 0;
	Context mContext;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.graphs);

		mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
		if (!mLibrary.load()) {
			finish();
		}
		mContext = this;
		GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
		gestures.addOnGesturePerformedListener(this);
		bar1 = (ImageView) findViewById(R.id.imageView2); // barra del q portes estalviat
		bar1.setAlpha(200);
		bar11 = (ImageView) findViewById(R.id.imageView4); // barra del que hauries de portar estalviat
		bar2 = (ImageView) findViewById(R.id.imageView3);
		bar22 = (ImageView) findViewById(R.id.imageView5); // groga q es mou
		bar22.setAlpha(200);
		imgconfig = (ImageView) findViewById(R.id.imageView6);

		imgconfig.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				startActivity(new Intent(mContext, Config.class));
				
			}
		});
		textviewgroc = (TextView) findViewById(R.id.textView1);
		textviewblau = (TextView) findViewById(R.id.textView2);
		textviewblau2 = (TextView) findViewById(R.id.textView3);

//		bar1.setOnLongClickListener(this);
//		bar11.setOnLongClickListener(this);
//		bar2.setOnLongClickListener(this);
//		bar22.setOnLongClickListener(this);
		
		OnClickListener click = new OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(mContext, ExpenseDetails.class));
			}
		};
		
		OnClickListener clickMonth = new OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(mContext, ExpenseDetailsMonth.class));
			}
		};
		
		bar1.setOnClickListener(click);
		bar11.setOnClickListener(click);
		bar2.setOnClickListener(clickMonth);
		bar22.setOnClickListener(clickMonth);
		
		
//		Dades inicials:
		textviewgroc.setText("" + datasamples.gastos[posactual] + " €");
		textviewblau.setText("" + datasamples.ingresos[posactual] + " €");
		textviewblau2.setText("" + posactual*120 + " €");
	}

	private void moversemana(boolean b) {
		if (b && datasamples.gastos.length > posactual) posactual++;
		else if (!b && posactual > 0)posactual--;
		try {
			DropDownAnim anim1 = new DropDownAnim(bar22, bar22.getHeight(), datasamples.gastos[posactual], bar2.getHeight()*2);
			textviewgroc.setText("" + datasamples.gastos[posactual] + " €");
			anim1.setFillAfter(true);
			anim1.setInterpolator(new BounceInterpolator());
			anim1.setDuration(1000);
			anim1.setAnimationListener(new AnimationListener() {
				
				public void onAnimationStart(Animation animation) {
					// TODO Auto-generated method stub
					
				}
				
				public void onAnimationRepeat(Animation animation) {
					// TODO Auto-generated method stub
					
				}
				
				public void onAnimationEnd(Animation animation) {
					if (bar22.getHeight() < bar2.getHeight()){
						bar22.setImageResource(R.drawable.barra_roja);
					} else {
						bar22.setImageResource(R.drawable.barra_amarilla);
					}					
				}
			});
			bar22.startAnimation(anim1);
			
			DropDownAnim anim2 = new DropDownAnim(bar1, bar1.getHeight(), datasamples.ingresos[posactual], bar2.getHeight()*2);
			textviewblau.setText("" + datasamples.ingresos[posactual] + " €");
			anim2.setFillAfter(true);
			anim2.setInterpolator(new BounceInterpolator());
			anim2.setDuration(1000);
			bar1.startAnimation(anim2);
			
			DropDownAnim anim3 = new DropDownAnim(bar11, bar11.getHeight(), posactual*120, bar2.getHeight()*2);
			textviewblau2.setText("" + posactual*120 + " €");
			anim3.setFillAfter(true);
			anim3.setInterpolator(new BounceInterpolator());
			anim3.setDuration(1000);
			bar11.startAnimation(anim3);
			
		} catch (IndexOutOfBoundsException e) {
			
		}
	}

	public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
		ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

		// We want at least one prediction
		if (predictions.size() > 0) {
			Prediction prediction = predictions.get(0);
			// We want at least some confidence in the result
			if (prediction.score > 1.0) {
				if (prediction.name.equals("slide-right")) {
					moversemana(true);
				} else if (prediction.name.equals("slide-left")) {
					moversemana(false);
				}
			}
		}
	}

//	public boolean onLongClick(View v) {
//		startActivity(new Intent(mContext, ExpenseDetails.class));
//		return true;
//	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	        if (posactual == 0) {
	        	return super.onKeyDown(keyCode, event);
	        } else {
		    	moversemana(false);
		        return true;
	        }
	    }
	    return super.onKeyDown(keyCode, event);
	}

}
